package com.north.light.libmediapreview;

import android.app.Activity;
import android.content.Intent;

import androidx.fragment.app.Fragment;

import com.north.light.libmediapreview.constant.PreviewIntentCode;
import com.north.light.libmediapreview.manager.PreviewLoadPicListener;
import com.north.light.libmediapreview.manager.PreviewLoadPicManager;
import com.north.light.libmediapreview.ui.PreviewPicLandscapeActivity;
import com.north.light.libmediapreview.ui.PreviewPicPortraitActivity;
import com.north.light.libmediapreview.ui.base.PreviewBaseParentActivity;

import java.io.Serializable;
import java.util.List;

/**
 * author:li
 * date:2022/9/25
 * desc:预览--外部调用入口
 */
public class PreviewManger implements Serializable {
    private Activity mContext;
    private PreviewParams mPreviewParams;

    public PreviewManger(Activity context) {
        this.mContext = context;
        this.mPreviewParams = new PreviewParams();
    }


    //构造方法----------------------------------------------------

    public static PreviewManger with(Activity context) {
        return new PreviewManger(context);
    }

    public static PreviewManger with(Fragment context) {
        return new PreviewManger(context.requireActivity());
    }

    public static PreviewManger with(android.app.Fragment context) {
        return new PreviewManger(context.getActivity());
    }

    public PreviewManger previewType(int type) {
        this.mPreviewParams.setType(type);
        return this;
    }

    public PreviewManger previewUrl(List<String> url) {
        this.mPreviewParams.setPreviewUrl(url);
        return this;
    }

    public PreviewManger previewOrg(int org) {
        this.mPreviewParams.setPreviewOrg(org);
        return this;
    }

    public PreviewManger previewBgResId(int resId) {
        this.mPreviewParams.setPreviewBgResId(resId);
        return this;
    }

    public PreviewManger previewCancelByOutSide(boolean cancel) {
        this.mPreviewParams.setPreviewCancelOutSide(cancel);
        return this;
    }

    public <C extends PreviewBaseParentActivity> PreviewManger previewClass(Class<C> act) {
        this.mPreviewParams.setPreviewClass(act);
        return this;
    }

    public PreviewManger start() {
        //完成构造，开始设置
        preview();
        return this;
    }


    private void preview() {
        if (mPreviewParams == null || mContext == null) {
            return;
        }
        if (mPreviewParams.getPreviewClass() != null &&
                mPreviewParams.getPreviewClass().getSuperclass() != PreviewBaseParentActivity.class) {
            //如果实现的类，没有继承指定集类，则直接返回
            return;
        }
        if (mPreviewParams.getPreviewClass() != null) {
            //自定义继承实现
            startActivity(mPreviewParams.getPreviewClass(), mPreviewParams);
            return;
        }
        int org = mPreviewParams.getPreviewOrg();
        switch (mPreviewParams.getType()) {
            case 0:
                //图片
                if (org == 0) {
                    //竖屏
                    previewPicPortrait(mPreviewParams);
                } else if (org == 1) {
                    //横屏
                    previewPicLandscape(mPreviewParams);
                }
                break;
            case 1:
                //视频
                if (org == 0) {
                    //竖屏
                    previewVideoPortrait(mPreviewParams);
                } else if (org == 1) {
                    //横屏
                    previewVideoLandscape(mPreviewParams);
                }
                break;
        }
    }


    /**
     * 预览图片--竖屏
     */
    private void previewPicPortrait(PreviewParams params) {
        startActivity(PreviewPicPortraitActivity.class, params);
    }

    /**
     * 预览图片--横屏
     */
    private void previewPicLandscape(PreviewParams params) {
        startActivity(PreviewPicLandscapeActivity.class, params);
    }

    /**
     * 预览视频--竖屏
     */
    private void previewVideoPortrait(PreviewParams params) {

    }

    /**
     * 预览视频--横屏
     */
    private void previewVideoLandscape(PreviewParams params) {
    }

    private void startActivity(Class<?> targetClass, PreviewParams params) {
        Intent targetIntent = new Intent(mContext, targetClass);
        targetIntent.putExtra(PreviewIntentCode.INTENT_PREVIEW_OBJ, params);
        targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(targetIntent);
    }

    //事件监听------------------------------------------------------------------

    public static void setOnPicLoadListener(PreviewLoadPicListener picListener) {
        PreviewLoadPicManager.getInstance().setListener(picListener);
    }

    public static void removePicLoadListener(PreviewLoadPicListener picListener) {
        PreviewLoadPicManager.getInstance().removeListener(picListener);
    }


}
